#include "key_process.h"
#include "pushrod_bsp.h"
#include "rgb.h"

key_para_t key_para;


static void Light_Handle(void)
{
	if (!key_para.light_press_cnt) {
		key_para.light_press_cnt = 1;
		Pwm_Sta_Set(ON);
	} else if (8 <= key_para.light_press_cnt) {
		key_para.light_press_cnt = 0;
		Pwm_Sta_Set(OFF);
	} else {
		key_para.light_press_cnt++;
		Pwm_Sta_Set(CHANGE);
	}
}


static void Head_Open_Handle(void)
{
	Pushrod_Opt(PUSHROD_DEVALL, PUSHROD_STOP);
	Pushrod_Opt(PUSH_DEV_HEAD, PUSHROD_OPEN);
}

static void Head_Close_Handle(void)
{
	Pushrod_Opt(PUSHROD_DEVALL, PUSHROD_STOP);
	Pushrod_Opt(PUSH_DEV_HEAD, PUSHROD_CLOSE);
}

static void Seat_Open_Handle(void)
{
	Pushrod_Opt(PUSHROD_DEVALL, PUSHROD_STOP);
	Pushrod_Opt(PUSH_DEV_SEAT, PUSHROD_OPEN);
}

static void Seat_Close_Handle(void)
{
	Pushrod_Opt(PUSHROD_DEVALL, PUSHROD_STOP);
	Pushrod_Opt(PUSH_DEV_SEAT, PUSHROD_CLOSE);
}

static void Foot_Open_Handle(void)
{
	Pushrod_Opt(PUSHROD_DEVALL, PUSHROD_STOP);
	Pushrod_Opt(PUSH_DEV_FOOT, PUSHROD_OPEN);
}

static void Foot_Close_Handle(void)
{
	Pushrod_Opt(PUSHROD_DEVALL, PUSHROD_STOP);
	Pushrod_Opt(PUSH_DEV_FOOT, PUSHROD_CLOSE);
}

static key_func_t key_func[] = {
    {KEY_K1_VALUE, Light_Handle},
    {KEY_K2_VALUE, Head_Open_Handle},
    {KEY_K3_VALUE, Head_Close_Handle},
    {KEY_K4_VALUE, Seat_Open_Handle},
    {KEY_K5_VALUE, Seat_Close_Handle},
    {KEY_K6_VALUE, Foot_Open_Handle},
    {KEY_K7_VALUE, Foot_Close_Handle},

};

void Key_Process_Init(void)
{
	memset(&key_para, 0, sizeof(key_para_t));
}

void Key_Handle(uint16_t cmd)
{
    if (key_para.cur_value != cmd) {
        key_para.cur_value = cmd;
        switch (key_para.cur_value) {
            case 0 : {
					Pushrod_Opt(PUSHROD_DEVALL, PUSHROD_STOP);
                break;
            }

            default : {
                unsigned char i;
                for (i = 0; i < sizeof(key_func) / sizeof(key_func[0]); i++) {
                    if (key_para.cur_value == key_func[i].index) {
                        if (key_func[i].func) {
                            key_func[i].func();
                        }
                    }
                }
                break;
            }
        }
    }
}

